/*
 * @Descripttion: 用户管理接口
 * @Author: chendingfengmail@163.com
 * @version:
 * @Date: 2021-05-26 16:53:40
 * @LastEditors: chendingfengmail@163.com
 * @LastEditTime: 2022-01-20 15:25:34
 */
import { request } from '../service'
import util from '@/libs/util.js'
const qs = require('qs');

const user = {

  /**
   * 获取用户信息
   * @returns {User} - 用户信息
   */
  getUserInfo() {
    return request({
      url: '/auth/user/info',
      method: 'get'
    });
  },

  /**
   * 获取系统预留手机验证码
   * @param {string} cellphone  - 手机号码
   * @param {string} userType   - 用户类型（非必传）
   * @returns
   */
  getVerifyCode({ cellphone, userType }) {
    return request({
      url: '/auth/user/getVerifyCode',
      method: 'GET',
      params: { cellphone, userType }
    })
  },

  /**
   * 通过验证码更新密码
   * @param {String} cellphone    - 手机号码
   * @param {String} verifyCode   - 手机验证码
   * @param {String} newPassword  - 新密码
   * @param {String} userType     - 用户类型（非必传）
   * @returns
   */
  updatePwdByVerifyCode({ cellphone, verifyCode, newPassword, userType }) {
    let param = { cellphone, verifyCode, newPassword:util.getmd5(newPassword + process.env.VUE_APP_MD5_SALT_VALUE), userType };
    return request({
      url: '/auth/user/updatePwdByVerifyCode',
      method: 'PUT',
      params: param
    })
  },

  /**
   * 查找用户列表
   * @param {json} param
   */
  findUserList(param) {
    return request({
      url: '/auth/user/findByAttributes',
      method: 'get',
      params: param
    })
  },

  /**
    * 查找用户信息
    * @param {String} id = 用户唯一标识
    */
  findUserById(id) {
    return request({
      url: `/auth/user/findById/${id}`,
      method: 'get',
    })
  },

  /**
   * 查找用户总数
   * @param {json} param
   */
  findUserListMax(param) {
    return request({
      url: '/auth/user/findMaxByAttributes',
      method: 'get',
      params: param
    })
  },

  /**
   * 添加用户信息
   * @param {json} param
   */
  addUser(param) {
    if(param.password) {
      param.password = util.getmd5(param.password + process.env.VUE_APP_MD5_SALT_VALUE)
    }
    return request({
      url: '/auth/user/add',
      method: 'post',
      data: addDefaultParam(param)
    })
  },

  /**
   * 删除用户
   * @param {String} id   - 用户唯一标识
   */
  delUser(id) {
    return request({
      url: `/auth/user/delete/${id}`,
      method: 'delete'
    })
  },

  /**
   * 修改用户
   * @param {json} param
   */
  updateUser(param) {
    if(param.password) {
      param.password = util.getmd5(param.password + process.env.VUE_APP_MD5_SALT_VALUE)
    }
    return request({
      url: '/auth/user/update',
      method: 'put',
      data: param
    })
  },

  /**
   * 批量修改用户锁定状态
   * @param {json} param
   */
   lockUnLock(param) {
    return request({
      url: '/auth/user/lockUnLock',
      method: 'put',
      params: param,
      paramsSerializer: params => {
        return qs.stringify(params, {
          indices: false
        })
      }
    })
  },

  /**
   * 批量删除用户信息(ids列表)
   * @param {json} param
   */
   batchDeleteUser(param) {
    return request({
      url: '/auth/user/deleteBatch',
      method: 'delete',
      params: param,
      paramsSerializer: params => {
        return qs.stringify(params, {
          indices: false
        })
      }
    })
  },

  /**
   * 绑定用户部门
   * @param {json} param
   */
  bindDepartment(param) {
    return request({
      url: '/auth/user/bindDepartment',
      method: 'put',
      data: param
    })
  },

  /**
   *  用户部门解绑
   * @param {json} param
   */
  unBindDepartment(param) {
    return request({
      url: '/auth/user/unBindDepartment',
      method: 'put',
      data: param
    })
  },

  /**
   * 修改密码
   * @param {String} oldPassword - 旧密码
   * @param {String} newPassword - 新密码
   */
  updatePassword({ oldPassword, newPassword }) {
    let param = { oldPassword:util.getmd5(oldPassword + process.env.VUE_APP_MD5_SALT_VALUE), newPassword:util.getmd5(newPassword + process.env.VUE_APP_MD5_SALT_VALUE) };
    return request({
      url: '/auth/user/updatePassword',
      method: 'put',
      params: param
    })
  },

  /**
   * 重置密码
   * @param {String} id         - 用户唯一标识符
   * @param {String} password   - 用户密码
   */
  resetPassword({ id, username, password }) {
    let param = { id, username, password:util.getmd5(password + process.env.VUE_APP_MD5_SALT_VALUE) };
    return request({
      url: '/auth/user/resetPwd',
      method: 'put',
      params: param
    })
  }

}

/**
 * 添加缺省参数值
 * @param {*} param 原始参数
 * @returns 携带缺省参数返回
 */
function addDefaultParam(param) {
  let params = param;
  if (params.username !== undefined && params.username == 'admin') {
    params['userType'] = 0;
  } else if(!params['userType']){
    params['userType'] = 1;
  }
  return params;
}

export default user;
